<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        button {
            width: 130px;
            height: 25px;
            margin-left: 200px;
            margin-bottom: 10px;
        }
        #box {
            width: 500px;
            /* height: 300px; */
            /* border: 1px solid #ccc; */
            display: flex;
        }
        #box div {
            width: 200px;
            height: 200px;
            background-color: slateblue;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <button id="add">点击将div变成红色</button>
    <div id="box">
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
<script>
    var btn = document.querySelector('#add')
    var di = document.querySelectorAll('#box div')

    btn.onclick = function() {
        for(var i=0 ; i < di.length; i++) {
            di[i].style.backgroundColor = 'purple'
        }
    }
</script>
</html>